/*
 * @lc app=leetcode.cn id=66 lang=java
 *
 * [66] 加一
 */

// @lc code=start
class Solution {
    
    /**
     * 111/111 cases passed (0 ms)
     * Your runtime beats 100 % of java submissions
     * Your memory usage beats 33.05 % of java submissions (39.9 MB)
     */
    public int[] plusOne(int[] digits) {
        int len = digits.length;
        int[] a = new int[len + 1];
        int factor = 1, cur = 0;
        while (cur < len) {
            int t = digits[len - 1 - cur] + factor;
            if (t >= 10) {
                factor = 1;
                a[cur] = 0;
            } else {
                factor = 0;
                a[cur] = t;
            }
            cur++;
        }
        if (factor > 0) {
            a[cur] = 1;
            len = len + 1;
        }
        int[] result = new int[len];
        for (int i = 0; i < len; i++) {
            result[i] = a[len - i - 1];
        }
        return result;
    }
}
// @lc code=end
